package modele;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
    ...
    @version 1.0
*/

public class MCompte {
    
    /** ... */
    private int id;
    
    /** ... */
    private float montantReel;
    
    /** ... */
    private float montantTheorique;
    
    /** ... */
    private float montantDebit;
    
    /** ... */
    private float montantCredit;
    
    /** ... */
    private Date dateCreation;
    
    /** ... */
    private ArrayList operations;
    
    /** ... */
    private int LastIdOperations;
    
    /**
        ...
        @param a ...
        @return ...
    */
    MCompte(int identifiant){
        super();
        this.id = identifiant;
        this.operations = new ArrayList();
        LastIdOperations = 0;
    }

    public int getId() {
        return id;
    }
    public Date getDateCreation() {
        return dateCreation;
    }
    public float getMontantReel() {
        return montantReel;
    }
    public void setMontantReel(float montantReel) {
        this.montantReel = montantReel;
    }
    public float getMontantTheorique() {
        return montantTheorique;
    }
    public void setMontantTheorique(float montantTheorique) {
        this.montantTheorique = montantTheorique;
    }
    public float getMontantDebit() {
        return montantDebit;
    }
    public void setMontantDebit(float montantDebit) {
        this.montantDebit = montantDebit;
    }
    public float getMontantCredit() {
        return montantCredit;
    }
    public void setMontantCredit(float montantCredit) {
        this.montantCredit = montantCredit;
    }
    
    /**
        ...
        @param a ...
        @return ...
    */
    public int ajoutOperation(Transaction operation, float montant, Date date, boolean effectuer){
        int error = 0;
        MOperation newOperation;;
        
        // MAJ des valeurs du compte
        if( operation == Transaction.CREDIT && effectuer ){
            montantReel += montant;
            montantTheorique += montant;
            montantCredit += montant;
        }else if (operation == Transaction.CREDIT && !effectuer){
            montantTheorique += montant;
            montantCredit += montant;
        }else if( operation == Transaction.DEBIT && effectuer){
            montantReel -= montant;
            montantTheorique -= montant;
            montantDebit += montant;
        }else if( operation == Transaction.DEBIT && !effectuer){
            montantTheorique -= montant;
            montantDebit += montant;
        }else{
            error = 1;
        }
        
        // Creation de l'operation
        newOperation = new MOperation(LastIdOperations+1,operation,montant,date,effectuer);
        
        // Ajout de l'operation dans la liste des operations
        if (!operations.add(newOperation)){
            error = 1;
        }
        
        return error;
    }
    
    /**
        ...
        @param a ...
        @return ...
    */
    public int suprimerOperation(int id){
        int error = 0;
        if( LastIdOperations <= id ){    
            operations.remove(id);
        }else{
            error = 1;
        }
        return error;
    }
    
    /**
        ...
        @param a ...
        @return ...
    */
    public void afficherOperations(){
        System.out.println("[INFO-OPERATIONS] Sur le compte numero"+this.id);
        for (Iterator<MOperation> it = operations.iterator(); it.hasNext();) {
            MOperation operation = it.next();
            operation.afficherOperation();
        }
    }
    /**
        ...
        @param a ...
        @return ...
    */
    public void afficherCompte(){
        System.out.println("[INFO-COMPTE] Numero "+this.id);
        System.out.println("Solde reel :"+this.montantReel);
        System.out.println("Solde théorique :"+this.montantTheorique);
    }
    
    
    
}
